package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenMingxiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxTzqkViewResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenMingxiService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLDecoder;


/**
 * 控制器
 *
 * @author modular/system
 * @Date 2020-11-19 14:18:37
 */
@Controller
@RequestMapping("/dgdzxChengbenMingxi")
public class DgdZxChengbenMingxiController extends BaseController {

    private Long pinpai = 1417361607353536514L;

    private String PREFIX = "/modular/system/dgdzxChengbenMingxi";

    @Autowired
    private ZxChengbenMingxiService zxChengbenMingxiService;

    @Resource
    private BtyBxdaService btyBxdaService;
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/zxChengbenMingxi.html";
    }
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("shenhe_index")
    public String shenhe_index() {
        return PREFIX + "/zxChengbenMingxi_shenhe_index.html";
    }

    /**
     * 跳转到主页面-直接添加报销明细页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/zxChengbenMingxiNew")
    public String index2(Model model) {
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        model.addAttribute("ckcheck", "");
        return PREFIX + "/zxChengbenMingxi_new.html";
    }

    /**
     * 跳转到主页面-直接添加报销明细页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/zxChengbenMingxiGhs")
    public String indexGhs(Model model) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
            bxda.setTel(account);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);

        return PREFIX + "/zxChengbenMingxi_ghs.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxChengbenMingxi_add.html";
    }

    /**
     * 批量调整门店
     *
     * @author zwf
     * @Date 2021-05-17
     */
    @RequestMapping("/batchTzMendian")
    public String batchTzMendian() {
        return PREFIX + "/zxChengbenMingxi_batchTzMendian.html";
    }

    /**
     * 批量调整门店
     *
     * @author zwf
     * @Date 2021-05-17
     */
    @RequestMapping("/batchSaveMendian")
    @ResponseBody
    public ResponseData batchSaveMendian() {
       String ids = getHttpServletRequest().getParameter("ids");
       String btyZxMdzxId = getHttpServletRequest().getParameter("btyZxMdzxId");
       String[] arr= ids.split(",");
       for(String id:arr){
           zxChengbenMingxiService.updateMendian(id,btyZxMdzxId);
       }
       return ResponseData.success();
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_edit.html";
    }


    /**
     * 编辑页面-直接添加报销单的编辑
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/editNew")
    public String editNew(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_editNew.html";
    }

    /**
     * 供货商编辑
     * @param btyZxKemuIdView
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/editGhs")
    public String editGhs(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
            bxda.setTel(account);
        }

//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/zxChengbenMingxi_editGhs.html";
    }

    /**
     * 审核
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/shenhe")
    public String shenhe(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_shenhe.html";
    }

    /**
     * 审核
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/chakan")
    public String chakan(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_chakan.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        //设置登录名
        return this.zxChengbenMingxiService.add(zxChengbenMingxiParam);
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/batchTransStatus")
    @ResponseBody
    public ResponseData batchTransStatus(ZxChengbenMingxiParam para) {
        para.setPinpai(pinpai);
        System.out.println(para.toString());
        String ids = para.getIds();
        String[] idsArr=  ids.split(",");
        for(String id:idsArr){
            Long idLong = Long.parseLong(id);
            ZxChengbenMingxi md = this.zxChengbenMingxiService.getById(idLong);
            md.setStatus(para.getStatus());
            this.zxChengbenMingxiService.updateById(md);
        }
        this.zxChengbenMingxiService.updateStatusNull();
        //设置登录名
        return ResponseData.success();
    }

    /**
     * 新增接口-直接添加报销单
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/addNewItem")
    @ResponseBody
    public ResponseData addNewItem(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        return this.zxChengbenMingxiService.addNewItem(zxChengbenMingxiParam);
    }

    /**
     * 批量新增接口-直接添加报销单
     *
     */
    @RequestMapping("/addMany")
    @ResponseBody
    public ResponseData addMany(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        return this.zxChengbenMingxiService.addMany(zxChengbenMingxiParam);
    }

    /**
     * 新增接口-直接添加报销单
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/addNewItemGhs")
    @ResponseBody
    public ResponseData addNewItemGhs(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        //判读是否维护银行卡和行号
        Long btyyBxdaKey = zxChengbenMingxiParam.getBtyyBxdaKey();
        BtyBxda bxda =  this.btyBxdaService.getById(btyyBxdaKey);
        if (bxda.getYhk()==null || bxda.getYhk().equals("")) {
            return new ErrorResponseData("银行卡号未空，请在供货商信息中维护银行卡号！");
        }
        if (bxda.getBz()==null || bxda.getBz().equals("")) {
            return new ErrorResponseData("开户行联行行号未空，请在供货商信息中维护开户行联行行号！！");
        }
        return this.zxChengbenMingxiService.addNewItemGhs(zxChengbenMingxiParam);
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        this.zxChengbenMingxiService.update(zxChengbenMingxiParam);
        return ResponseData.success();
    }



    /**
     *
     * 科目同步
     *
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/kemuTongbu")
    @ResponseBody
    public ResponseData kemuTongbu(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        ResponseData responseData = this.zxChengbenMingxiService.kemuTongbu(zxChengbenMingxiParam);
        return responseData;
    }
    /**
     * 供货商修改
     *
     * @author zwf
     * @Date 2021-01-16
     *
     */
    @RequestMapping("/editItem2")
    @ResponseBody
    public ResponseData editItem2(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        this.zxChengbenMingxiService.update(zxChengbenMingxiParam);
        return ResponseData.success();
    }

    /**
     * 审核提交
     *
     * @author zwf
     * @Date 2021-01-18
     *
     */
    @RequestMapping("/shenheTijiao")
    @ResponseBody
    public ResponseData shenheTijiao(ZxChengbenMingxiParam zxChengbenMingxiParam) throws Exception{
        zxChengbenMingxiParam.setPinpai(pinpai);
        BigDecimal jine = zxChengbenMingxiParam.getJine();
        BigDecimal bxjine = zxChengbenMingxiParam.getBxjine();
        if(jine.doubleValue()>bxjine.doubleValue()){
            return ResponseData.error("确认报销金额不能大约报销金额");
        }
        this.zxChengbenMingxiService.shenheTijiao(zxChengbenMingxiParam);
        return ResponseData.success();
    }
    /**
     * 供货商修改
     *
     * @author zwf
     * @Date 2021-01-16
     *
     */
    @RequestMapping("/editItemGhs")
    @ResponseBody
    public ResponseData editItemGhs(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        ResponseData responseData = this.zxChengbenMingxiService.updateGhs(zxChengbenMingxiParam);
        return responseData;
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        boolean isDel = this.zxChengbenMingxiService.delete(zxChengbenMingxiParam);
        if(isDel==false){
            return new ErrorResponseData("删除失败");
        }
        return ResponseData.success();
    }

    /**
     * 抽点
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/choudian")
    @ResponseBody
    public ResponseData choudian(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        boolean isDel = this.zxChengbenMingxiService.choudian(zxChengbenMingxiParam);
        return ResponseData.success();
    }

    /**
     * 批量抽点
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/choudianBatch")
    @ResponseBody
    public ResponseData choudianBatch(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        String ids = zxChengbenMingxiParam.getIds();
        String[] idsArr=  ids.split(",");
        for(String id:idsArr){
            zxChengbenMingxiParam.setBtyZxChengbenMingxiId(Long.parseLong(id));
            this.zxChengbenMingxiService.choudian(zxChengbenMingxiParam);
        }
        return ResponseData.success();
    }

    /**
     * 不抽点
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/buchoudian")
    @ResponseBody
    public ResponseData buchoudian(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        boolean isDel = this.zxChengbenMingxiService.buchoudian(zxChengbenMingxiParam);
        return ResponseData.success();
    }
    /**
     * 不抽点
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/buchoudianBatch")
    @ResponseBody
    public ResponseData buchoudianBatch(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        String ids = zxChengbenMingxiParam.getIds();
        String[] idsArr=  ids.split(",");
        for(String id:idsArr){
            zxChengbenMingxiParam.setBtyZxChengbenMingxiId(Long.parseLong(id));
            boolean isDel = this.zxChengbenMingxiService.buchoudian(zxChengbenMingxiParam);
        }
        return ResponseData.success();
    }


    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/deleteGhs")
    @ResponseBody
    public ResponseData deleteGhs(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        boolean isDel = this.zxChengbenMingxiService.deleteGhs(zxChengbenMingxiParam);
        if(isDel==false){
            return new ErrorResponseData("删除失败");
        }
        return ResponseData.success();
    }


    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        ZxChengbenMingxi detail = this.zxChengbenMingxiService.getById(zxChengbenMingxiParam.getBtyZxChengbenMingxiId());
        if(detail.getStatus()==null || detail.getStatus().equals("")){
            detail.setStatus("2");
        }
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        return this.zxChengbenMingxiService.findPageBySpec(zxChengbenMingxiParam);
    }
    /**
     * 投资情况表[明细]列表
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @ResponseBody
    @RequestMapping("/tzqkMingXiList")
    public LayuiPageInfo tzqkMingXiList(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        zxChengbenMingxiParam.setTzqk_flag("1");
        return this.zxChengbenMingxiService.findPageBySpec(zxChengbenMingxiParam);
    }

    /**
     * 投资情况表[明细]列表
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @ResponseBody
    @RequestMapping("/queryCreateUser")
    public ResponseData queryCreateUser() {
        return this.zxChengbenMingxiService.queryCreateUser();
    }


    @RequestMapping("/obtainFromBaoxiaodan")
    @ResponseBody
    public ResponseData obtainFromBaoxiaodan(ZxTzqkViewResult param) {
        return this.zxChengbenMingxiService.obtainFromBaoxiaodan(param);
    }

    @RequestMapping("/btnImportMdbx")
    @ResponseBody
    public ResponseData btnImportMdbx(@RequestParam("file") MultipartFile file,@RequestParam("btyZxChengbenId") Long btyZxChengbenId){
        try {
            ResponseData result = this.zxChengbenMingxiService.btnImportMdbx(file,btyZxChengbenId);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new ErrorResponseData("解析文件异常");
        }
    }


    /**
     * 投资情况-一键批量获取
     * @param param
     * @return
     */
    @RequestMapping("/createDakuandan")
    @ResponseBody
    public ResponseData onClickobtainAll(ZxChengbenMingxiParam param) {
        return this.zxChengbenMingxiService.createDakuandan(param);
    }

    /**
     *  提交报销
     * @param param
     * @return
     */
    @RequestMapping("/tijiaobaoxiaoGhs")
    @ResponseBody
    public ResponseData tijiaobaoxiaoGhs(ZxChengbenMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxChengbenMingxiService.tijiaobaoxiaoGhs(param);
    }

    /**
     *  审批 单号的所有报销单
     * @param param
     * @return
     * by zwf 2021-01-20
     */
    @RequestMapping("/shenpiByDanhao")
    @ResponseBody
    public ResponseData shenpiByDanhao(ZxChengbenMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxChengbenMingxiService.shenpiByDanhao(param);
    }

    /**
     * 提交报销(被打回)
     * @param param
     * @return
     */
    @RequestMapping("/tijiaobaoxiaoGhsTh")
    @ResponseBody
    public ResponseData tijiaobaoxiaoGhsTh(ZxChengbenMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxChengbenMingxiService.tijiaobaoxiaoGhsTh(param);
    }

    @RequestMapping("/export")
    public ResponseEntity<byte[]> export(HttpServletRequest request, HttpServletResponse response, ZxChengbenMingxiParam param){
        return this.zxChengbenMingxiService.exportMx(request,response,param);
    }


    /**
     * 合并门店
     *
     */
    @RequestMapping("/mergeMendian")
    @ResponseBody
    public ResponseData mergeMendian(ZxChengbenMingxiParam zxChengbenMingxiParam) {
        zxChengbenMingxiParam.setPinpai(pinpai);
        String ids = zxChengbenMingxiParam.getIds();
        String[] idsArr=  ids.split(",");
        for(String id:idsArr){
            zxChengbenMingxiParam.setBtyZxChengbenMingxiId(Long.parseLong(id));
            this.zxChengbenMingxiService.mergeMendian(zxChengbenMingxiParam);
        }
        return ResponseData.success();
    }

    /**
     * 合并门店
     *
     */
    @RequestMapping("/hebingMendian")
    public String hebingMendian() {
        return PREFIX + "/zxChengbenMingxi_merge.html";
    }

}


